//
//  LoginBabyDataViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/1.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "LoginBabyDataViewController.h"

@interface LoginBabyDataViewController ()

@property (nonatomic, strong) UIButton *maleBtn;
@property (nonatomic, strong) UIButton *famaleBtn;
@property (nonatomic, strong) UILabel *yearLabel;
@property (nonatomic, strong) UILabel *monthLabel;
@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) UITextField *textfield;
@property (nonatomic, copy) NSString *dateString;
@end

@implementation LoginBabyDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写宝宝信息";
    _dateString = [GW_Tool getLocalDateString];
    [self addRightBarButtonWithTitle:@"完成" andSel:@selector(handleComplete)];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[IMAGE(@"close") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(handleBtn)];
    
    self.navigationItem.leftBarButtonItem = btn;
    [self initView];
    
    NSArray *dateArray = [_dateString componentsSeparatedByString:@"-"];
    _yearLabel.text = dateArray.firstObject;
    _monthLabel.text = dateArray[1];
    _dateLabel.text = dateArray[2];
}

//点击叉号的响应方法
- (void)handleBtn
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//完成按钮相应方法
- (void)handleComplete
{
    NSString *gender;
    if(_maleBtn.selected == YES)
    {
        gender = @"1";
    }
    else
    {
        gender = @"2";
    }
    NSDictionary *params = @{@"babyName":_textfield.text, @"babySex": gender, @"babyBrithday":_dateString};
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/common/SetBaby?babyName=%@&babySex=%@&babyBirthday=%@", GW_Domain, _textfield.text, gender, _dateString] parameters:params completionBlockWithSuccess:^(NSDictionary *resultDict) {
        [self loginSuccessBlock];
        [GW_Tool saveLoginInfoWithDict:resultDict[@"data"]];
        [self loginSuccessAndCloseVC];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        MESSAGE_TOAST(errorMsg);
        NSLog(@"%@", error);
    }];
}

- (void)loginSuccessAndCloseVC {
    
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        if (self.loginSuccessBlock) {
            self.loginSuccessBlock();
        }
    }];
}

- (void)initView
{
    UIImageView *iv = [UIImageView new];
    iv.image = IMAGE(@"baby_log");
    [self.view addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        if(IS_IPHONE5)
        {
            make.top.equalTo(self.view).offset(10);
        }
        else
            make.top.equalTo(self.view).offset(29);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@88);
        make.height.equalTo(@88);
    }];
    
    _textfield = [UITextField new];
    _textfield.placeholder = @"andy";
    [_textfield setValue:ColorFrom16A(0xc1c1c1, 1) forKeyPath:@"_placeholderLabel.textColor"];//通过kvc去改变占位符的字体颜色
    [_textfield setValue:PFMFONT(18) forKeyPath:@"_placeholderLabel.font"];
    _textfield.autocorrectionType = UITextAutocorrectionTypeNo;
    UIImageView *image = [UIImageView new];
    image.frame = CGRectMake(0, 0, 20, 20);
    image.image = IMAGE(@"login_textfield");
    _textfield.leftView = image;
    _textfield.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_textfield];
    [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(16);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.greaterThanOrEqualTo(@72);
        make.height.equalTo(@25);
    }];
    
    _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_maleBtn setTitle:@"男" forState:0];
    [_maleBtn setImage:IMAGE(@"mine_male") forState:0];
    _maleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    [_maleBtn setTitleColor:ColorFrom16(0x2A2F33) forState:0];
    _maleBtn.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    _maleBtn.selected = YES;
    _maleBtn.layer.cornerRadius = 15;
    [_maleBtn addTarget:self action:@selector(handleMaleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_maleBtn];
    [_maleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(61.2);
        make.left.equalTo(self.view).offset(102);
        make.width.equalTo(@76);
        make.height.equalTo(@30);
    }];
    
    _famaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_famaleBtn setTitle:@"女" forState:0];
    [_famaleBtn setImage:IMAGE(@"mine_famale") forState:0];
    _famaleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    [_famaleBtn setTitleColor:ColorFrom16(0x2A2F33) forState:0];
    _famaleBtn.layer.backgroundColor = ColorFrom16(0xf7f7f7).CGColor;
    _famaleBtn.layer.cornerRadius = 15;
    [_famaleBtn addTarget:self action:@selector(handleFamale:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_famaleBtn];
    [_famaleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(61.2);
        make.left.equalTo(_maleBtn.mas_right).offset(20);
        make.width.equalTo(@76);
        make.height.equalTo(@30);
    }];
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = ColorFrom16(0xFFF3C8);
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        if(IS_IPHONE5)
            make.top.equalTo(_maleBtn.mas_bottom).offset(39);
        else
            make.top.equalTo(_maleBtn.mas_bottom).offset(79);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@54.8);
    }];
    UIView *vw = [UIView new];
    vw.backgroundColor = ColorFrom16(0xF7E4A1);
    [self.view addSubview:vw];
    [vw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_bottom);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@20);
    }];
    
    _yearLabel = [UILabel new];
    _yearLabel.text = @"2017";
    _yearLabel.textColor = ColorFrom16(0xffffff);
    _yearLabel.textAlignment = 1;
    _yearLabel.backgroundColor = ColorFrom16(0xFEDE6D);
    if(IS_IPHONE5)
        _yearLabel.font = [UIFont fontWithName:@"Avenir-Heavy" size:24];
    else
        _yearLabel.font = [UIFont fontWithName:@"Avenir-Heavy" size:28];
    [self.view addSubview:_yearLabel];
    
    NSDictionary *dic = @{NSKernAttributeName:@6.73f
                          
                          };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_yearLabel.text attributes:dic];
    _yearLabel.attributedText = attributedString;
    [_yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if(IS_IPHONE5)
        {
            make.top.equalTo(_maleBtn.mas_bottom).offset(20);
            make.width.equalTo(@80);
        }
        else
        {
            make.width.equalTo(@104);
            make.top.equalTo(_maleBtn.mas_bottom).offset(52);
        }
        make.height.equalTo(@58);
        make.left.equalTo(self.view).offset(10);
    }];
    UIView *yearView = [UIView new];
    yearView.backgroundColor = ColorFrom16(0xFF9C48);
    [self.view addSubview:yearView];
    [yearView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yearLabel.mas_bottom);
        make.left.equalTo(self.view).offset(10);
        if(IS_IPHONE5)
            make.width.equalTo(@80);
        else
            make.width.equalTo(@104);
        make.height.equalTo(@10);
    }];
    
    _monthLabel = [UILabel new];
    _monthLabel.text = @"09";
    _monthLabel.textColor = ColorFrom16(0xffffff);
    _monthLabel.textAlignment = 1;
    _monthLabel.backgroundColor = ColorFrom16(0xFEDE6D);
    _monthLabel.font = [UIFont fontWithName:@"Avenir-Heavy" size:28];
    [self.view addSubview:_monthLabel];
    attributedString = [[NSMutableAttributedString alloc] initWithString:_monthLabel.text attributes:dic];
    _monthLabel.attributedText = attributedString;
    [_monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yearLabel);
        make.left.equalTo(_yearLabel.mas_right).offset(11);
        if(IS_IPHONE5)
        {
            make.width.equalTo(@60);
        }
        else
        {
            make.width.equalTo(@80);
        }
        make.height.equalTo(@58);
    }];
    
    UIImageView *flowerImage = [UIImageView new];
    flowerImage.image = IMAGE(@"baby_flower");
    [self.view addSubview:flowerImage];
    [flowerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_monthLabel);
        make.right.equalTo(self.view).offset(-6);
        if(IS_IPHONE5)
        {
            make.width.equalTo(@50);
            make.height.equalTo(@67);
        }
        else
        {
            make.width.equalTo(@58);
            make.height.equalTo(@75);
        }
    }];
    
    UIView *monthView = [UIView new];
    monthView.backgroundColor = ColorFrom16(0xFF9C48);
    [self.view addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_monthLabel.mas_bottom);
        make.left.equalTo(_yearLabel.mas_right).offset(11);
        if(IS_IPHONE5)
            make.width.equalTo(@60);
        else
            make.width.equalTo(@80);
        make.height.equalTo(@10);
    }];
    
    _dateLabel = [UILabel new];
    _dateLabel.text = @"01";
    _dateLabel.textColor = ColorFrom16(0xffffff);
    _dateLabel.textAlignment = 1;
    _dateLabel.backgroundColor = ColorFrom16(0xFEDE6D);
    _dateLabel.font = [UIFont fontWithName:@"Avenir-Heavy" size:28];
    [self.view addSubview:_dateLabel];
    attributedString = [[NSMutableAttributedString alloc] initWithString:_dateLabel.text attributes:dic];
    _dateLabel.attributedText = attributedString;
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yearLabel);
        make.left.equalTo(_monthLabel.mas_right).offset(11);
        make.height.equalTo(@58);
        make.width.equalTo(@80);
    }];
    
    UIView *dateView = [UIView new];
    dateView.backgroundColor = ColorFrom16(0xFF9C48);
    [self.view addSubview:dateView];
    [dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_monthLabel.mas_bottom);
        make.left.equalTo(monthView.mas_right).offset(11);
        make.width.equalTo(@80);
        make.height.equalTo(@10);
    }];
    
    UIImageView *aImage = [UIImageView new];
    aImage.image = IMAGE(@"baby_date");
    [self.view addSubview:aImage];
    [aImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).offset(128);
        make.centerX.equalTo(self.view.mas_centerX).offset(-60);
        if(IS_IPHONE5)
            make.top.equalTo(vw.mas_bottom).offset(13);
        else
            make.top.equalTo(vw.mas_bottom).offset(33);
        make.width.equalTo(@19.9);
        make.height.equalTo(@19.9);
    }];
    
    UILabel *birthdayLabel = [UILabel new];
    birthdayLabel.text = @"选择宝宝生日";
    birthdayLabel.textColor = ColorFrom16(0xF6D04A);
    birthdayLabel.font = PFRFONT(14);
    [self.view addSubview:birthdayLabel];
    [birthdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(aImage);
        make.left.equalTo(aImage.mas_right).offset(12);
        make.width.equalTo(@88);
        make.height.equalTo(@20);
    }];
    
    UIDatePicker *datePicker = [UIDatePicker new];
    //设置显示方式  这里只显示年月日
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.height = 38;
    datePicker.maximumDate = [NSDate date];//设置最大时间为今天
    [datePicker addTarget:self action:@selector(datePickerValueChanged:)forControlEvents:UIControlEventValueChanged];
    //设置本地化支持的语言（在此是中文)
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    [self.view addSubview:datePicker];
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-50);
        make.left.equalTo(self.view).offset(22);
        make.right.equalTo(self.view).offset(-22);
        make.height.equalTo(@114);
    }];
}

- (void)datePickerValueChanged:(UIDatePicker *)sender
{
    NSDate *date = sender.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    
    NSString  *string = [[NSString alloc]init];
    string = [dateFormatter stringFromDate:date];
    _dateString = string;
    NSArray *dateArray = [string componentsSeparatedByString:@"-"];
    _yearLabel.text = dateArray.firstObject;
    _monthLabel.text = dateArray[1];
    _dateLabel.text = dateArray[2];
}

- (void)handleMaleBtn:(UIButton *)sender
{
    sender.selected = YES;
    _famaleBtn.selected = NO;
    sender.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    _famaleBtn.layer.backgroundColor = ColorFrom16(0xf7f7f7).CGColor;
}

- (void)handleFamale:(UIButton *)sender
{
    sender.selected = YES;
    _maleBtn.selected = NO;
    sender.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    _maleBtn.layer.backgroundColor = ColorFrom16(0xf7f7f7).CGColor;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    if (self.closeLoginVCBlock) {
        self.closeLoginVCBlock();
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
